var appInstance = getApp();
var pageData    = {
  data: {
    keyword:'',
    shade:true,
    array:['微信','发短信','生成pdf'],  
    picker:{'li':[{'img':'../../images/shelve.png','txt':'上架','eventParams':'shelve',"eventHandler":"picker"},{'img':'../../images/off_shelve.png','txt':'下架','eventParams':'off_shelve',"eventHandler":"picker"},{'img':'../../images/delete.png','txt':'删除','eventParams':'delete',"eventHandler":"picker"},]}, 
    currentTab: 0, 
    navbarm:{
      hot:[{'m':'行业','id':'0'},{'m':'行业','id':'1'},{'m':'行业','id':'2'},{'m':'行业','id':'3'}],
      city:[{'m':'城市','id':'0'},{'m':'城市','id':'1'},{'m':'城市','id':'2'},{'m':'城市','id':'3'}],
    }, 
    free_vessel4:{"content":[{"content":"http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg",'product_name':'已上架','company_name':'company_name',"eventParams":"a","eventHandler":"tapInnerLinkHandler",'status':false},{"content":"../../images/add.png",'product_name':'','company_name':'　',"eventParams":["add"],"eventHandler":"tapInnerLinkHandler"}],
    'pageNo':0}
  },
  app_title: '石雕小程序',
  app_description: '加油!',
  page_router: 'page10000',
  onLoad: function (e) {
    var that =this;
    this.searchPrdList();
     },
  onShareAppMessage: function(){
    var pageRouter = this.page_router;
    var param = appInstance.globalData.session_key;
    var vendorUserId = param.userId;
    return {
      title: this.app_title || '产品设计',
      desc: this.app_description || '',
      path: '/pages/'+pageRouter+'/'+pageRouter+'?vendorUserId='+vendorUserId
    }
  },
  onShow: function(){ 
  },
  navbarTap: function(e){
    var id = e.currentTarget.dataset.idx;
    var navbar = this.data.navbar;
    this.setData({  
      currentTab: id  
    })
  },
  searchinput:function(e){
    var keyword = e.detail.value;
    this.setData({
      keyword:keyword
    })
  },
  searchPrdList:function(){
    var that = this;
    var pageNo = this.data.free_vessel4.pageNo;
    var param = appInstance.globalData.session_key;
    var keyword = this.data.keyword;
    var searchPrdList ={
      token :param.myXcxSessionId,
      source:'1',
      target : '2',    
      userId:param.userId,
      //行业分类 bizCatlg 行业细类 subCatlg 城市 cityId 关键字 keyword 
      bizCatlg:'',
      subCatlg:'',
      cityId:'',
      keyword:keyword||'', 
      pageNo:pageNo,
      pageSize:'',    
    }
    appInstance.sendRequest({
      url:'/prod/searchPrdList',
      data:searchPrdList,
      method:'POST',
      success: function(res){
        var data = res.userdata,
            pageN = res.page;
        var arr = [];
        for(var i=0,len=data.length;i<len;i++){
          var dataset = {}
          var coverImg = data[i].coverImg;
          dataset.content= coverImg;
          dataset.product_name=data[i].prodStatus=='4'?'已上架':'未上架' ;
          dataset.company_name=data[i].productName;
          dataset.putOnDate=data[i].putOnDate;
          dataset.eventParams=[data[i].productId,data[0].companyId];
          dataset.eventHandler='tapInnerLinkHandler';
          dataset.status=false;
          arr.push(dataset);
        }
        var add = {"content":"http://oh4xneden.bkt.clouddn.com/tmp_1859413202o6zAJs1cHTCyKqdVf28s9WnUD35kb9246207723b9401fbc3c5f936488557.png",'product_name':'','company_name':'　',"eventParams":["add"],"eventHandler":"tapInnerLinkHandler"} ;
        arr.push(add);                 
        that.setData({
           free_vessel4:{"content":arr,'pageNo':pageN} 
        })
      },
      fail: function(res){
        console.log('getmode fail');
      },
      complete: function(res){
      }
    }) 
  },
  picker:function(e){
    let that = this;     
    var tapeven = e.currentTarget.dataset.eventParams;
    console.log(tapeven);
    var prods=[];
    var content = that.data.free_vessel4.content;
    for(var i=0,len=content.length;i<len-1;i++){
      var _content = content[i];
      if(_content.status==true){
        prods.push({'productId':_content.eventParams[0]})
      }
    }
    if(prods.length==0){
      appInstance.showModal({
        content:'请先选择产品'
      })
      return false;
    }
    var param = appInstance.globalData.session_key;
    var prod_param = {
        prods:prods,
        source:'1',
        token:param.myXcxSessionId
        }
    console.log(prod_param)
    that.setData({
        shade:true
      })
    if(tapeven=='delete'){
       appInstance.sendRequest({
              url:'/prod/delProds',
              data:prod_param,
              method:'POST', 
              header:{
              'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();                     
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }else if(tapeven=='shelve'){
      appInstance.sendRequest({
              url:'/prod/putOnProd',
              data:prod_param,
              method:'POST',
              header:{
              'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();                       
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }else if(tapeven=='off_shelve'){
      appInstance.sendRequest({
              url:'/prod/pullOffProd',
              data:prod_param,
              method:'POST', 
              header:{
              'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();                      
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }
  },
  pick_show:function(){
    var that =this;
    var boolean_set = !that.data.shade;
    console.log(boolean_set);
    //make it hide
     if(boolean_set==true){
      var arr = that.data.free_vessel4.content;
      for(var i=0,len=arr.length;i<len-1;i++){        
          arr[i].status=false;
      };
      var pageNo = this.data.free_vessel4.pageNo;
      that.setData({
        free_vessel4:{"content":arr,'pageNo':pageNo}, 
        shade:boolean_set
       })
     }else{
      that.setData({
        shade:boolean_set
      })
     }
  },
  chooseAll:function(){
    var that =this;
    var boolean_set = !that.data.shade;
    console.log(boolean_set);
    var arr = that.data.free_vessel4.content;
     //make it hide
     var status = arr[0].status
     if(boolean_set==true){        
        for(var i=0,len=arr.length;i<len-1;i++){
          //逻辑需要更细        
            arr[i].status=!status;
        };
        var pageNo = this.data.free_vessel4.pageNo;
        that.setData({
          free_vessel4:{"content":arr,'pageNo':pageNo,}, 
        })
     }else{
        for(var i=0,len=arr.length;i<len-1;i++){        
            arr[i].status=true;
        };
        var pageNo = this.data.free_vessel4.pageNo;
        that.setData({
          free_vessel4:{"content":arr,'pageNo':pageNo}, 
          shade:boolean_set
        })
     }
  },
  //长按待选项
  tapInnerLinkHandler:function(e){
    let that = this;    
    if(that.data.longtap == true){
      that.setData({
        shade:false,
        longtap:false
      })
      return false;
    } 
    var tapeven = e.currentTarget.dataset.eventParams[0];
    console.log(tapeven)
    if(that.data.shade == false){
      var arr = that.data.free_vessel4.content;
      for(var i=0,len=arr.length;i<len-1;i++){
        if(arr[i].eventParams[0] == tapeven){         
            arr[i].status=!arr[i].status;
        }
      };
      var pageNo = this.data.free_vessel4.pageNo;
      that.setData({
        free_vessel4:{"content":arr,'pageNo':pageNo}, 
      })
    }else{
      if(tapeven=='add'){
          wx.navigateTo({
            url: '../addproduct/addproduct',
            success: function(res){
              // success
            },
          })
        }else{
        wx.navigateTo({
            url: '../productdetail/productdetail?prodId='+tapeven+'&isSplot=true',
            success: function(res){
              // success
          },
        })
      }    
      // var touchTime = that.data.touch_end - that.data.touch_start;  
      // console.log(touchTime);  
      // if (touchTime > 350) {}  
   }
  },
   //longtap  
 mytouchstart: function () {  
   let that = this;  
   that.setData({  
     longtap:true
   })  
 }     
};
Page(pageData);